const web = require("../../module/web"), loger = require("../../module/loger");

module.exports.run = ()=>{
	web.run((w)=>{
		let uf = w.window.document.getElementById("userinfo");
		if(uf){
			let al = uf.getElementsByTagName("a");
			for(let i=0;i<al.length;i++){
				if(al[i].innerHTML == '登录'){
					 al[i].click();
					 return;
				}else if(al[i].innerHTML == '注册'){
					//al[i].click();
					//return;
				}else if(al[i].innerHTML == '退出'){ //账号已经登录了
					let inp = w.window.document.getElementById("page-yx").getElementsByTagName("a");
					if(inp && inp[0]) inp[0].click();
					//global.sdObj.end = 0;
				}
			}				
		}
	}, (w)=>{
		loger.info("login loaded");
		if(w.window.location.href.indexOf("http://i.360.cn/login") != -1){
			let sperr = w.window.document.getElementsByClassName("quc-tip quc-tip-error");
			for(let i=0;i<sperr.length;i++){
				if(sperr[i].innerHTML == "请输入验证码"){ 
					loger.info("login need yzm");
					web.getImg('tmp', 3000, ()=>{
						global.sdObj.end = 0;
					})
					return true;
				}else if(sperr[i].innerHTML == "帐号不存在"){
					global.sdObj.end = 1032;
					return true;
				}else if(sperr[i].innerHTML == "登录密码错误，请重新输入"){
					global.sdObj.end = 1033;
					return true;
				}
			}
			let inp = w.window.document.getElementsByName("account"); //quc-input quc-input-captcha
			if(inp && inp[0]) inp[0].value = global.sdObj.ud[3];
			inp = w.window.document.getElementsByName("password");
			if(inp && inp[0]) inp[0].value = global.sdObj.ud[10];
			inp = w.window.document.getElementsByClassName("quc-submit quc-button quc-button-sign-in");
			if(inp && inp[0]) inp[0].click();
		}else{
			let uf = w.window.document.getElementById("userinfo");
			if(uf){
				let al = uf.getElementsByTagName("a");
				for(let i=0;i<al.length;i++){
					if(al[i].innerHTML == '退出'){ //账号已经登录了
						global.sdObj.end = -110;
						return true;
					}
				}				
			}
		}
	})
}